package kuangshen.start;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

//实现Callable接口
public class CallableTest  implements Callable {
    public CallableTest(String url, String name) {
        this.url = url;
        this.name = name;
    }

    private String url;
    private String name;
    @Override
    public Object call() throws Exception {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.downLoader(url,name);
        System.out.println("下载了文件："+name);
        return true;
    }

    /**
     * 可以定义返回值
     * 可以抛出异常
     * @param args
     */
    public static void main(String[] args) {
        //创建服务
        CallableTest callableTest1 = new CallableTest("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1963304009,2816364381&fm=26&gp=0.jpg","1.jpg");
        CallableTest callableTest2 = new CallableTest("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1963304009,2816364381&fm=26&gp=0.jpg","2.jpg");
        CallableTest callableTest3 = new CallableTest("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1963304009,2816364381&fm=26&gp=0.jpg","3.jpg");

        ExecutorService executorService = Executors.newFixedThreadPool(3);

        Future<Boolean> r1 = executorService.submit(callableTest1);
        Future<Boolean> r2 = executorService.submit(callableTest2);
        Future<Boolean> r3 = executorService.submit(callableTest3);

        System.out.println("下载结果"+r1);
        System.out.println("下载结果"+r2);
        System.out.println("下载结果"+r3);
        executorService.shutdown();
    }
}
